﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CommunicatorHistoryViewer.Models;
using System.Runtime.InteropServices;
using System.Windows.Markup;
using System.IO;

namespace CommunicatorHistoryViewer.Controls
{
    public partial class HistoryView : Window
    {
        private Color color;
        protected ContactViewModel ViewModel { get; set; }

        public HistoryView(ContactViewModel viewModel)
        {
            InitializeComponent();
            this.ViewModel = viewModel;
            this.ContactName.Content = viewModel.Name;

            FilterHistory.Items.Add(new { Text = "Today", Value = 0 });
            FilterHistory.Items.Add(new { Text = "1 week before", Value = 7 });
            FilterHistory.Items.Add(new { Text = "1 month before", Value = 30 });
            FilterHistory.Items.Add(new { Text = "3 monthes before", Value = 90 });
            FilterHistory.Items.Add(new { Text = "All history", Value = -1 });

            FilterHistory.DisplayMemberPath = "Text";
            FilterHistory.SelectedValuePath = "Value";
            FilterHistory.SelectionChanged += new SelectionChangedEventHandler(FilterHistory_SelectionChanged);
            FilterHistory.SelectedIndex = 1;

            InitColor();
        }

        void FilterHistory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var days = Convert.ToInt32(FilterHistory.SelectedValue);
            WebBrowserOverlayWF wbo = new WebBrowserOverlayWF(this.HistoryBrowserConteiner);
            System.Windows.Forms.WebBrowser wb = wbo.WebBrowser;
            if (days < 0)
            {
                wb.DocumentText = this.ViewModel.HistoryManager.GetHistoryForContact(ViewModel.Name);
            }
            else
            {
                wb.DocumentText = this.ViewModel.HistoryManager.GetHistoryForContact(ViewModel.Name, DateTime.Now.AddDays(-days).Date);
            }
            wb.DocumentCompleted += (s, arg) =>
                {
                    
                    System.Windows.Forms.WebBrowser browser = s as System.Windows.Forms.WebBrowser;
                    browser.Focus();
                    System.Windows.Forms.SendKeys.Send("{DOWN}");
               };

        }


        #region [ Methods ]

        protected void InitColor()
        {
            Random rnd = new Random();
            color = Color.FromRgb((byte)rnd.Next(0, 256), (byte)rnd.Next(0, 256), (byte)rnd.Next(0, 256));

            this.TopRectangle.Fill = new SolidColorBrush(this.color);
            this.LeftRectangle.Fill = new SolidColorBrush(this.color);
        }

        #endregion

        #region [ Events ]

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void ButtonClose_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void ButtonClose_MouseEnter(object sender, MouseEventArgs e)
        {
            ButtonClose.Foreground = new SolidColorBrush(this.color);
        }

        private void ButtonClose_MouseLeave(object sender, MouseEventArgs e)
        {
            ButtonClose.Foreground = new SolidColorBrush(Color.FromRgb(80, 80, 80));
        }

        #endregion
    }
}
